package com.e.dianshang.service.impl;

import com.e.dianshang.mapper.IndexMapper;
import com.e.dianshang.service.IndexService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author 陈静
 * @date 2020/11/11 11:17
 */
@Service("indexService")
public class IndexServiceImpl implements IndexService {
    @Resource
    IndexMapper indexMapper;

    /**
     *  显示默认搜索的商品
     * @param opedId 电话号码
     * @return 默认搜索商品
     */
//    @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED,rollbackFor = {Throwable.class})
    @Override
    public String defaultSearch(String opedId) {
        return indexMapper.defaultSearch(opedId);
    }

    /**
     *  搜索商品
     * @param goods 商品或者品牌
     * @return 商品详情
     */
    @Override
    public List<Map> search(String goods) {
        List<Map> search = indexMapper.search(goods);
        for (Map map : search) {
            Integer goodsId = (Integer) map.get("goods_id");
            List<Map> goodsAttribute = indexMapper.getGoodsAttribute(goodsId);
            String brandName = (String) map.get("brand_name");
            List<Map> ticket = indexMapper.getTicket(brandName);
           map.put("attribute",goodsAttribute);
            map.put("tickets",ticket);
        }
        return search;
    }

    /**
     *  推荐商品
     * @param openID 电话号码
     * @return 商品信息
     */
    @Override
   public List<Map> recommendGoods(String openID){
        Integer id = indexMapper.recommendGoodsId(openID);
        List<Map> list = indexMapper.recommendGoods(id);
        for (Map map : list) {
            Integer goodsId = (Integer) map.get("goods_id");
            List<Map> goodsAttribute = indexMapper.getGoodsAttribute(goodsId);
            String brandName = (String) map.get("brand_name");
            List<Map> ticket = indexMapper.getTicket(brandName);
            map.put("attribute",goodsAttribute);
            map.put("tickets",ticket);
        }
        return list;
   }

    @Override
    public List<Map> timeUpGoods() {
        List<Map> list = indexMapper.timeUpGoods();
        for (Map map : list) {
            Integer goodsId = (Integer) map.get("goods_id");
            List<Map> goodsAttribute = indexMapper.getGoodsAttribute2(goodsId);
            map.put("attribute",goodsAttribute);
        }
        return list;
    }
}
